package com.ut.richpath.model;

import com.ut.richpath.util.XmlParser;
import ohos.agp.utils.Matrix;
import ohos.app.Context;
import ohos.global.resource.solidxml.Attribute;
import ohos.org.xml.sax.Attributes;

import java.util.List;

public class Group {

    public static final String TAG_NAME = "group";

    private float rotation = 0f;
    private float pivotX = 0f;
    private float pivotY = 0f;
    private float scaleX = 1f;
    private float scaleY = 1f;
    private float translateX = 0f;
    private float translateY = 0f;
    private String name;
    private Matrix matrix;

    public float getRotation() {
        return rotation;
    }

    public void setRotation(float rotation) {
        this.rotation = rotation;
    }

    public float getPivotX() {
        return pivotX;
    }

    public void setPivotX(float pivotX) {
        this.pivotX = pivotX;
    }

    public float getPivotY() {
        return pivotY;
    }

    public void setPivotY(float pivotY) {
        this.pivotY = pivotY;
    }

    public float getScaleX() {
        return scaleX;
    }

    public void setScaleX(float scaleX) {
        this.scaleX = scaleX;
    }

    public float getScaleY() {
        return scaleY;
    }

    public void setScaleY(float scaleY) {
        this.scaleY = scaleY;
    }

    public float getTranslateX() {
        return translateX;
    }

    public void setTranslateX(float translateX) {
        this.translateX = translateX;
    }

    public float getTranslateY() {
        return translateY;
    }

    public void setTranslateY(float translateY) {
        this.translateY = translateY;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Matrix getMatrix() {
        return matrix;
    }

    public void setMatrix(Matrix matrix) {
        this.matrix = matrix;
    }

    public Group(Context context, Attributes attributes) {
        inflate(context, attributes);
    }

    public Group(Context context, List<Attribute> attributes) {
        inflate(context, attributes);
    }



    private void inflate(Context context, Attributes attributes) {

        name = XmlParser.getAttributeString(attributes, "name", name);

        rotation = XmlParser.getAttributeFloat(attributes, "rotation", rotation);

        scaleX = XmlParser.getAttributeFloat(attributes, "scaleX", scaleX);

        scaleY = XmlParser.getAttributeFloat(attributes, "scaleY", scaleY);

        translateX = XmlParser.getAttributeFloat(attributes, "translateX", translateX);

        translateY = XmlParser.getAttributeFloat(attributes, "translateY", translateY);

        pivotX = XmlParser.getAttributeFloat(attributes, "pivotX", pivotX) + translateX;

        pivotY = XmlParser.getAttributeFloat(attributes, "pivotY", pivotY) + translateY;
    }



    private void inflate(Context context, List<Attribute> attributes) {

        name = XmlParser.getAttributeString(attributes, "name", name);

        rotation = XmlParser.getAttributeFloat(attributes, "rotation", rotation);

        scaleX = XmlParser.getAttributeFloat(attributes, "scaleX", scaleX);

        scaleY = XmlParser.getAttributeFloat(attributes, "scaleY", scaleY);

        translateX = XmlParser.getAttributeFloat(attributes, "translateX", translateX);

        translateY = XmlParser.getAttributeFloat(attributes, "translateY", translateY);

        pivotX = XmlParser.getAttributeFloat(attributes, "pivotX", pivotX) + translateX;

        pivotY = XmlParser.getAttributeFloat(attributes, "pivotY", pivotY) + translateY;
    }


    public Matrix matrix(){
        Matrix matrix;
        if (this.matrix==null){
            this.matrix = new Matrix();
            this.matrix.postTranslate(-pivotX, -pivotY);
            this.matrix.postScale(scaleX, scaleY);
            this.matrix.postRotate(rotation, 0f, 0f);
            this.matrix.postTranslate(translateX + pivotX, translateY + pivotY);
        }
        matrix = this.matrix;
        return matrix;
    }


    public void scale(Matrix matrix) {
        matrix().postConcat(matrix);
    }

}
